package com.coocare.licence.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.coocare.licence.config.domain.AjaxResult;
import com.coocare.licence.config.plugins.logging.annotation.Logging;
import com.coocare.licence.entity.SysMenu;
import com.coocare.licence.service.ISysMenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author adam
 * @since 2023-10-10
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final ISysMenuService sysMenuService;

    /**
     * 返回当前用户的树形菜单集合
     * @param type 类型
     * @param parentId 父节点ID
     * @return 当前用户的树形菜单
     */
    @GetMapping
    public AjaxResult getUserMenu(String type, Long parentId) {
        // 获取符合条件的菜单
        Set<SysMenu> all = new HashSet<>();
        StpUtil.getRoleList().forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(Long.valueOf(roleId))));
        return AjaxResult.ok(sysMenuService.filterMenu(all, type, parentId));
    }

    /**
     * 返回树形菜单集合
     * @param parentId 父节点ID
     * @param menuName 菜单名称
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public AjaxResult getTree(Long parentId, String menuName, String type) {
        return AjaxResult.ok(sysMenuService.treeMenu(parentId, menuName, type));
    }

    /**
     * 返回角色的菜单集合
     * @param roleId 角色ID
     * @return 属性集合
     */
    @GetMapping("/tree/{roleId}")
    public AjaxResult getRoleTree(@PathVariable Long roleId) {
        return AjaxResult.ok(sysMenuService.findMenuByRoleId(roleId).stream().map(SysMenu::getMenuId).collect(Collectors.toList()));
    }

    /**
     * 通过ID查询菜单的详细信息
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.ok(sysMenuService.getById(id));
    }

    /**
     * 新增菜单
     * @param sysMenu 菜单信息
     * @return success/false
     */
    @Logging("新增菜单")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('sys_menu_add')")
    public AjaxResult save(@Valid @RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return AjaxResult.ok(sysMenu);
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return success/false
     */
    @Logging("删除菜单")
    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('sys_menu_del')")
    public AjaxResult removeById(@PathVariable Long id) {
        return sysMenuService.removeMenuById(id);
    }

    /**
     * 更新菜单
     * @param sysMenu
     * @return
     */
    @Logging("更新菜单")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('sys_menu_edit')")
    public AjaxResult update(@Valid @RequestBody SysMenu sysMenu) {
        return AjaxResult.ok(sysMenuService.updateMenuById(sysMenu));
    }
}
